<template>
	<basic-container>
		<avue-crud
			:option="option"
			:data="data"
			:page="page"
			ref="crud"
			@search-change="searchChange"
			@search-reset="searchReset"
			@current-change="currentChange"
			@size-change="sizeChange"
			@row-del="rowDel"
			@row-update="rowUpdate"
			@row-save="rowSave"
			@on-load="onLoad"
		>
			<template slot="menuRight">
				<el-radio-group @change="searchReset" v-model="status" size="small" style="margin-right: 50px;">
					<el-radio-button :label="item.code" v-for="(item, i) in statusItem" :key="i">{{ item.name }}</el-radio-button>
				</el-radio-group>
			</template>
		</avue-crud>
	</basic-container>
</template>

<script>
import request from '@/router/axios';
import { getCodes } from '@/api/system/dict';

export default {
	data() {
		return {
			statusItem: [
				{
					code: '1',
					type: '',
					name: '业务'
				},
				{
					code: '0',
					type: 'info',
					name: '系统'
				}
			],
			status: '1',
			query: {},
			page: {
				pageSize: 10,
				currentPage: 1,
				total: 0
			},
			option: {
				searchMenuSpan: 4,
				border: true,
				index: true,
				refreshBtn: false,
				excelBtn: true,
				menuWidth: '150px',
				column: [
					{
						label: '编号名称',
						prop: 'name',
						search: true,
						rules: [
							{
								required: true,
								trigger: 'blur'
							}
						]
					},
					{
						label: '前缀',
						prop: 'prefix',
						width: '150px',
						rules: [
							{
								required: true,
								trigger: 'blur'
							}
						]
					},
					{
						label: '日期格式',
						prop: 'ymd',
						width: '150px',
						type: 'select',
						dicData: [],
						props: {
							label: 'name',
							value: 'name'
						}
					},
					{
						label: '后缀位数',
						type: 'number',
						width: '100px',
						prop: 'suffix',
						rules: [
							{
								required: true,
								trigger: 'blur'
							}
						]
					},
					{
						label: '当前值',
						prop: 'current',
						display: false
					}
				]
			},
			data: []
		};
	},
	created() {
		getCodes('c62f4321-b8dd-43c0-aeb3-77eac695d314').then(res => {
			this.findObject(this.option.column, 'ymd').dicData = res.data.data;
		});
	},
	methods: {
		rowSave(row, loading, done) {
			row.current = this.status
			request({
				url: '/api/BaseSerial/add',
				method: 'post',
				data: row
			}).then(
				() => {
					loading();
					this.onLoad(this.page);
					done();
				},
				error => {
					done();
					console.log(error);
				}
			);
		},
		rowUpdate(row, index, loading, done) {
			row.current = this.status
			return request({
				url: '/api/BaseSerial/edit',
				method: 'post',
				data: row
			}).then(
				() => {
					loading();
					this.onLoad(this.page);
				},
				error => {
					done();
					console.log(error);
				}
			);
		},
		rowDel(row) {
			this.$confirm('确定数据删除?', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					return request({
						url: '/api/BaseSerial/remove',
						method: 'post',
						params: {
							id: row.id,
							typ:this.status
						}
					});
				})
				.then(() => {
					this.onLoad(this.page);
				});
		},
		searchReset() {
			this.query = {};
			this.page.currentPage = 1;
			this.onLoad(this.page);
		},
		searchChange(params, done) {
			this.page.currentPage = 1;
			this.query = params;
			this.onLoad(this.page);
			done();
		},
		currentChange(currentPage) {
			this.page.currentPage = currentPage;
		},
		sizeChange(pageSize) {
			this.page.pageSize = pageSize;
		},
		onLoad(page) {
			request({
				url: '/api/BaseSerial/getpages',
				method: 'get',
				params: {
					key: this.query.name == undefined ? '' : this.query.name,
					type: this.status == undefined ? '' : this.status,
					page: page.currentPage,
					limit: page.pageSize
				}
			}).then(res => {
				const data = res.data;
				this.data = data.data;
				this.page.total = data.remark;
			});
		}
	}
};
</script>

<style></style>
